package com.leap.meeting.dao.imple;

import com.leap.meeting.dao.BaseDAO;
import com.leap.meeting.dao.UserDAO;
import com.leap.meeting.pojo.User;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

public class UserDAOimple extends BaseDAO implements UserDAO {
    public List<User> selectAll() {
        try {
            String sql = "SELECT id,account,real_name AS realName,apartment,phone,email,password,duty from user";
            return super.executeQuery(User.class, sql,null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public User selectByAccount(String account) {
        try {
            String sql = "SELECT id,account,real_name AS realName,apartment,phone,email,password,duty from user where account = ?";
            return super.executeQuerySingle(User.class, sql,account);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public User selectByName(String name) {
        try {
            String sql = "SELECT id,account,real_name AS realName,apartment,phone,email,password,duty from user where real_name = ?";
            return super.executeQuerySingle(User.class, sql,name);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public int update(User user) {
        String sql = "UPDATE user SET account = ?, real_name = ?, apartment = ?, phone = ?, email = ?, password = ?, duty = ? WHERE id = ?";
        return super.executeUpdate(sql,user.getAccount(),user.getRealName(),user.getApartment(),user.getPhone(),user.getEmail(),user.getPassword(),user.getDuty(),user.getId());
    }

    @Override
    public int insert(User user) {
        String sql = "INSERT INTO user(account, real_name, apartment, phone, email, password, duty) VALUES(?,?,?,?,?,?,?)";
        return super.executeUpdate(sql,user.getAccount(),user.getRealName(),user.getApartment(),user.getPhone(),user.getEmail(),user.getPassword(),user.getDuty());
    }

}
